home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Graphics / QD3D / rollercoaster / Sources / Win32Application.c < prev   
Encoding:
C/C++ Source or Header  |  2000-09-28  |  15.3 KB  |  441 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Win32Application.c
  3.  
  4.     Contains:    Standard Win32 event loop & window creation code
  5.  
  6.     Written by:    Scott Kuechle
  7.  
  8.     Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <2>        9/28/98        rtm        made changes for Metrowerks compiler
  13.         <1>         9/01/98        srk        first file
  14.         
  15.  
  16.  NOTES:
  17.  
  18.  
  19.  TO DO:
  20.  
  21. */
  22.  
  23. /************************************************************
  24. *                                                           *
  25. *    INCLUDE FILES                                          *
  26. *                                                           *
  27. *************************************************************/
  28.  
  29. #include "Win32Application.h"
  30.  
  31. /************************************************************
  32. *                                                           *
  33. *    GLOBAL VARIABLES                                       *
  34. *                                                           *
  35. *************************************************************/
  36.  
  37. HINSTANCE            ghInst;              /* current instance */
  38. DocumentRec            gDocument;
  39. UINT                gTimer;
  40. char szAppName[] = "RollerCoaster";        /* The name of this application */
  41. char szTitle[]   = "RollerCoaster";        /* The title bar text */
  42. COLORREF            customColors[16] = { 0L };            /* for use by choosecolor dialog */
  43.  
  44. /************************************************************
  45. *                                                           *
  46. *    CONSTANTS                                              *
  47. *                                                           *
  48. *************************************************************/
  49.  
  50. #define kMillsPerTick 10  /* milliseconds per timer tick */
  51.  
  52.  
  53. /************************************************************
  54. *                                                           *
  55. *    FUNCTION PROTOTYPES                                    *
  56. *                                                           *
  57. *************************************************************/
  58.  
  59. static BOOL InitApplication(HINSTANCE hInstance);
  60. static BOOL InitInstance( HINSTANCE hInstance, int nCmdShow);
  61. static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM uParam, LPARAM lParam);
  62. BOOL CenterWindow (HWND hwndChild, HWND hwndParent);
  63. static LRESULT CALLBACK About(
  64.                                 HWND hDlg,           /* window handle of the dialog box */
  65.                                 UINT message,        /* type of message */
  66.                                 WPARAM uParam,       /* message-specific information */
  67.                                 LPARAM lParam);
  68. void        UpdateFrame(void);
  69. TQ3Status    GetToolBarPosition(DocumentPtr theDocument, unsigned long *x, unsigned long *y);
  70. TQ3Status    GetToolBarSize(DocumentPtr theDocument, unsigned long *width, unsigned long *height);
  71.  
  72. static void StartTimer( void );
  73. static void StopTimer( void );
  74. VOID CALLBACK TimerProc(
  75.                         HWND hwnd,    /* handle of window for timer messages */
  76.                         UINT uMsg,    /* WM_TIMER message */
  77.                         UINT idEvent,    /* timer identifier */
  78.                         DWORD dwTime     /* current system time */
  79.                        );
  80.  
  81. /************************************************************
  82. *                                                           *
  83. *    FUNCTION:  WinMain                                     *
  84. *                                                           *
  85. *    PURPOSE:   Standard Windows WinMain routine            *
  86. *                                                           *
  87. *************************************************************/
  88.  
  89. int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  90.                     LPSTR lpCmdLine, int nCmdShow)
  91. {
  92.     MSG msg;
  93.     HANDLE hAccelTable;
  94.  
  95.         if (!InitApplication(hInstance)) 
  96.         {
  97.             return (FALSE);    
  98.         }
  99.  
  100.         if (!InitInstance(hInstance, nCmdShow)) 
  101.         {
  102.             return (FALSE);
  103.         }
  104.  
  105.         StartTimer();
  106.  
  107.         hAccelTable = LoadAccelerators (hInstance, MAKEINTRESOURCE(IDR_GENERIC));
  108.  
  109.         while (GetMessage(&msg, NULL, 0, 0))
  110.         {
  111.             if (!TranslateAccelerator (msg.hwnd, hAccelTable, &msg))
  112.             {
  113.                 TranslateMessage(&msg);
  114.                 DispatchMessage(&msg);
  115.             }
  116.         }
  117.  
  118.         StopTimer();
  119.  
  120.         return (msg.wParam); /* Returns the value from PostQuitMessage */
  121. }
  122.  
  123. /************************************************************
  124. *                                                           *
  125. *    FUNCTION:  UpdateFrame                                 *
  126. *                                                           *
  127. *    PURPOSE:   Invalidates our window so it will be        *
  128. *               redrawn                                     *
  129. *                                                           *
  130. *************************************************************/
  131.  
  132. void UpdateFrame(void)
  133. {
  134.     RECT            aWinRect;
  135.     BOOL            aResult;
  136.  
  137.         aResult = GetClientRect(gDocument.fMainWindow, (LPRECT)&aWinRect);
  138.         aResult = InvalidateRect(gDocument.fMainWindow, &aWinRect, FALSE);    
  139. }
  140.  
  141.  
  142. /************************************************************
  143. *                                                           *
  144. *    FUNCTION:  InitApplication                             *
  145. *                                                           *
  146. *    PURPOSE:   Invalidates our window so it will be        *
  147. *               redrawn                                     *
  148. *                                                           *
  149. *************************************************************/
  150.  
  151. BOOL InitApplication(HINSTANCE hInstance)
  152. {
  153.     WNDCLASS  wc;
  154.  
  155.             /* CS_OWNDC is required to support the Win32DC Draw context */
  156.         wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  157.  
  158.         wc.lpfnWndProc   = (WNDPROC)WndProc;       
  159.         wc.cbClsExtra    = 0;                      
  160.         wc.cbWndExtra    = 0;                     
  161.         wc.hInstance     = hInstance;             
  162.         wc.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_APP)); 
  163.         wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  164.         wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  165.         wc.lpszMenuName  = MAKEINTRESOURCE(IDR_GENERIC); 
  166.         wc.lpszClassName = szAppName;              
  167.  
  168.         return (RegisterClass(&wc));
  169. }
  170.  
  171. /************************************************************
  172. *                                                           *
  173. *    FUNCTION:  InitInstance                                *
  174. *                                                           *
  175. *    PURPOSE:   Creates our window                          *
  176. *                                                           *
  177. *                                                           *
  178. *************************************************************/
  179.  
  180. BOOL InitInstance(
  181.                     HINSTANCE       hInstance,
  182.                     int             nCmdShow)
  183. {
  184.     HWND  hWnd; 
  185.  
  186.         ghInst = hInstance; 
  187.         hWnd = CreateWindowEx(
  188.                 WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,
  189.                 szAppName,          
  190.                 szTitle,             
  191.                 WS_OVERLAPPEDWINDOW, 
  192.                 25, 25, 640, 530,      /* fixed size windows */
  193.                 NULL,                
  194.                 NULL,                
  195.                 hInstance,           
  196.                 NULL                 
  197.         );
  198.  
  199.         if (!hWnd) 
  200.         {
  201.             return (FALSE);
  202.         }
  203.  
  204.  
  205.         ShowWindow(hWnd, nCmdShow); 
  206.         UpdateWindow(hWnd);        
  207.  
  208.         return (TRUE);             
  209.  
  210. }
  211.  
  212. /************************************************************
  213. *                                                           *
  214. *    FUNCTION:  WndProc                                     *
  215. *                                                           *
  216. *    PURPOSE:   Our window procedure                        *
  217. *                                                           *
  218. *                                                           *
  219. *************************************************************/
  220.  
  221. LRESULT CALLBACK WndProc(
  222.                         HWND hWnd,        
  223.                         UINT message,      
  224.                         WPARAM uParam,     
  225.                         LPARAM lParam
  226.                         )   
  227. {
  228.     int wmId, wmEvent;
  229.     PAINTSTRUCT PaintStruct;
  230.  
  231.         switch (message) 
  232.         {
  233.             case WM_COMMAND:
  234.  
  235.                 wmId    = LOWORD(uParam);
  236.                 wmEvent = HIWORD(uParam);
  237.  
  238.                 switch (wmId) 
  239.                 {
  240.                     case IDM_ABOUT:
  241.                         DialogBox(ghInst,          
  242.                                 MAKEINTRESOURCE(IDD_ABOUTBOX),
  243.                                 hWnd,                 
  244.                                 (DLGPROC)About);
  245.                         break;
  246.  
  247.                     case IDM_EXIT:
  248.                         DestroyWindow (hWnd);
  249.                         break;
  250.  
  251.                     default:
  252.                         return (DefWindowProc(hWnd, message, uParam, lParam));
  253.                 }
  254.                 break;
  255.  
  256.             case WM_CREATE:
  257.                 QD3DSupport_InitDoc3DData( hWnd, &gDocument );
  258.                 break;
  259.  
  260.             case WM_DESTROY:  /* message: window being destroyed */
  261.                 PostQuitMessage(0);
  262.                 QD3DSupport_DisposeDoc3DData( &gDocument );
  263.                 break;
  264.  
  265.             case WM_PAINT:
  266.                 BeginPaint(gDocument.fMainWindow, &PaintStruct);                
  267.                 QD3DSupport_DocDraw3DData( &gDocument );
  268.                 EndPaint(gDocument.fMainWindow, &PaintStruct);
  269.                 break;
  270.  
  271.             case WM_SIZE:
  272.                 {
  273.                 unsigned long width = LOWORD(lParam);  /* width of client area */
  274.                 unsigned long height = HIWORD(lParam); /* height of client area */
  275.  
  276.                 break;
  277.                 }
  278.  
  279.             case WM_QUERYENDSESSION:
  280.                 return (TRUE);
  281.  
  282.             default:          /* Passes it on if unproccessed */
  283.                 return (DefWindowProc(hWnd, message, uParam, lParam));
  284.         }
  285.         return (0);
  286. }
  287.  
  288.  
  289. /************************************************************
  290. *                                                           *
  291. *    FUNCTION:  CenterWindow                                *
  292. *                                                           *
  293. *    PURPOSE:   Place our window on the screen              *
  294. *                                                           *
  295. *                                                           *
  296. *************************************************************/
  297.  
  298. BOOL CenterWindow (HWND hwndChild, HWND hwndParent)
  299. {
  300.     RECT    rChild, rParent;
  301.     int     wChild, hChild, wParent, hParent;
  302.     int     wScreen, hScreen, xNew, yNew;
  303.     HDC     hdc;
  304.  
  305.         /* Get the Height and Width of the child window */
  306.         GetWindowRect (hwndChild, &rChild);
  307.         wChild = rChild.right - rChild.left;
  308.         hChild = rChild.bottom - rChild.top;
  309.  
  310.         /* Get the Height and Width of the parent window */
  311.         GetWindowRect (hwndParent, &rParent);
  312.         wParent = rParent.right - rParent.left;
  313.         hParent = rParent.bottom - rParent.top;
  314.  
  315.         /* Get the display limits */
  316.         hdc = GetDC (hwndChild);
  317.         wScreen = GetDeviceCaps (hdc, HORZRES);
  318.         hScreen = GetDeviceCaps (hdc, VERTRES);
  319.         ReleaseDC (hwndChild, hdc);
  320.  
  321.         /* Calculate new X position, then adjust for screen */
  322.         xNew = rParent.left + ((wParent - wChild) /2);
  323.         if (xNew < 0)
  324.         {
  325.             xNew = 0;
  326.         }
  327.         else if ((xNew+wChild) > wScreen)
  328.         {
  329.             xNew = wScreen - wChild;
  330.         }
  331.  
  332.         /* Calculate new Y position, then adjust for screen */
  333.         yNew = rParent.top  + ((hParent - hChild) /2);
  334.         if (yNew < 0)
  335.         {
  336.             yNew = 0;
  337.         }
  338.         else if ((yNew+hChild) > hScreen)
  339.         {
  340.             yNew = hScreen - hChild;
  341.         }
  342.  
  343.         /* Set it, and return */
  344.         return SetWindowPos (hwndChild, NULL,
  345.                 xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  346. }
  347.  
  348. /************************************************************
  349. *                                                           *
  350. *    FUNCTION:  About                                       *
  351. *                                                           *
  352. *    PURPOSE:   About box callback                          *
  353. *                                                           *
  354. *                                                           *
  355. *************************************************************/
  356.  
  357. LRESULT CALLBACK About(
  358.                     HWND hDlg,           /* window handle of the dialog box */
  359.                     UINT message,        /* type of message */
  360.                     WPARAM uParam,       /* message-specific information */
  361.                     LPARAM lParam)
  362. {
  363.     static  HFONT hfontDlg;
  364.     LPSTR   lpVersion;
  365.     DWORD   dwVerInfoSize;
  366.     DWORD   dwVerHnd;
  367.     UINT    uVersionLen;
  368.     WORD    wRootLen;
  369.     BOOL    bRetCode;
  370.     int     i;
  371.     char    szFullPath[256];
  372.     char    szResult[256];
  373.     char    szGetName[256];
  374.  
  375.         switch (message)
  376.         {
  377.                 case WM_INITDIALOG:  /* message: initialize dialog box */
  378.                         return (TRUE);
  379.  
  380.                 case WM_COMMAND:                      /* message: received a command */
  381.                         if (LOWORD(uParam) == IDOK        /* "OK" box selected? */
  382.                         || LOWORD(uParam) == IDCANCEL) {  /* System menu close command? */
  383.                                 EndDialog(hDlg, TRUE);        /* Exit the dialog */
  384.                                 DeleteObject (hfontDlg);
  385.                                 return (TRUE);
  386.                         }
  387.                         break;
  388.         }
  389.         return (FALSE); /* Didn't process the message */
  390. }
  391.  
  392. /************************************************************
  393. *                                                           *
  394. *    FUNCTION:  StartTimer                                  *
  395. *                                                           *
  396. *    PURPOSE:   Create a timer which will be used to        *
  397. *               periodically call an update routine for     *
  398. *               our window.                                 *
  399. *                                                           *
  400. *                                                           *
  401. *************************************************************/
  402.  
  403. static void StartTimer( void )
  404. {
  405.     gTimer = SetTimer( NULL, 0, kMillsPerTick, (TIMERPROC) TimerProc );
  406. }
  407.  
  408. /************************************************************
  409. *                                                           *
  410. *    FUNCTION:  StopTimer                                   *
  411. *                                                           *
  412. *    PURPOSE:   Stops our timer                             *
  413. *                                                           *
  414. *                                                           *
  415. *************************************************************/
  416.  
  417. static void StopTimer( void )
  418. {
  419.     KillTimer( NULL, gTimer );
  420. }
  421.  
  422. /************************************************************
  423. *                                                           *
  424. *    FUNCTION:  TimerProc                                   *
  425. *                                                           *
  426. *    PURPOSE:   Our timer procedure, which calls and        *
  427. *               update routine for our window               *
  428. *                                                           *
  429. *                                                           *
  430. *************************************************************/
  431.  
  432. VOID CALLBACK TimerProc(
  433.                         HWND hwnd,    /* handle of window for timer messages  */
  434.                         UINT uMsg,    /* WM_TIMER message */
  435.                         UINT idEvent,    /* timer identifier */
  436.                         DWORD dwTime     /* current system time */
  437.                        )    
  438. {
  439.     UpdateFrame();
  440. }
  441.